package com.lyc.lojbackendquestionservice.controller.inner;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lyc.lojbackendmodel.model.entity.Question;
import com.lyc.lojbackendmodel.model.entity.QuestionSubmit;
import com.lyc.lojbackendquestionservice.service.QuestionService;
import com.lyc.lojbackendquestionservice.service.QuestionSubmitService;
import com.lyc.lojbackendserviceclient.service.QuestionFeignClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 该服务仅内部调用，不是给前端的
 */
@RestController
@RequestMapping("/inner")

public class QuestionInnerController implements QuestionFeignClient {
    
    @Resource
    private QuestionService questionService;
    @Resource
    private QuestionSubmitService questionSubmitService;
    
    
    @Override
    @GetMapping("/get/id")
    public Question getQuestionById(@RequestParam("questionId") long questionId){
        return questionService.getById(questionId);
    }
    
    @Override
    @GetMapping("/question_submit/get/id")
    public QuestionSubmit getQuestionSubmitById(@RequestParam("questionSubmitId") long questionSubmitId){
        return questionSubmitService.getById(questionSubmitId);
    }
    
    @Override
    @PostMapping("/question_submit/update")
    public boolean updateQuestionSubmitById(@RequestBody QuestionSubmit questionSubmit){
        return questionSubmitService.updateById(questionSubmit);
    }

    @Override
    @PostMapping("question_acceptedNum/update")
    public boolean updateQuestionAcceptedNumById(@RequestParam("questionId") long questionId) {
        LambdaUpdateWrapper<Question> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Question::getId,questionId);
        lambdaUpdateWrapper.setSql("acceptedNum = acceptedNum + 1");
        return questionService.update(lambdaUpdateWrapper);
    }

    @PostMapping("/question/update")
    @Override
    public boolean updateQuestionById(@RequestBody Question question) {
        return questionService.updateById(question);
    }
}
